package com.sc.nft.dao;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.sc.nft.entity.DaoSchoolChild;
import com.sc.nft.entity.dto.DaoSchoolChildPageAdminDTO;
import com.sc.nft.entity.dto.DaoSchoolChildTopGetListAllDTO;
import com.sc.nft.entity.dto.DaoSchoolChildUpdateByIdAdminDTO;
import com.sc.nft.entity.vo.DaoSchoolArticleMaterialTopIdVO;
import com.sc.nft.entity.vo.DaoSchoolChildPageAdminVO;
import com.sc.nft.mapper.DaoSchoolChildMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * dao学堂子分类表(DaoSchoolChild)表Dao层
 *
 * @author makejava
 * @since 2023-03-13 16:21:50
 */
@Slf4j
@Repository
public class DaoSchoolChildDao extends ServiceImpl<DaoSchoolChildMapper, DaoSchoolChild> {

    /**
     * 分页获取1级分类列表
     *
     * @param pageRequest
     * @return
     */
    public Page<DaoSchoolChildPageAdminVO> pageAdmin(DaoSchoolChildPageAdminDTO pageRequest) {
        return baseMapper.pageAdmin(pageRequest.toMp(),pageRequest);
    }

    public Boolean selectByNameCountIsExist(String name,Long topId) {
        QueryWrapper<DaoSchoolChild> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("second_top_id", topId);
        queryWrapper.eq("dao_id", 6L);
        Integer selectCount = baseMapper.selectCount(queryWrapper);
        return SqlHelper.retBool(selectCount);
    }

    /**
     * 判断除了自身id的name,其他的名字是否有重复
     *
     * @param name
     * @param id
     * @return
     */
    public Boolean selectByNameCountIsExistNotId(String name, Long id,Long topId) {
        QueryWrapper<DaoSchoolChild> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", name);
        queryWrapper.eq("dao_id", 6L);
        queryWrapper.eq("second_top_id", topId);
        queryWrapper.ne("id", id);
        Integer selectCount = baseMapper.selectCount(queryWrapper);
        return SqlHelper.retBool(selectCount);
    }

    public Boolean updateById(DaoSchoolChildUpdateByIdAdminDTO daoSchoolChildUpdateByIdAdminDTO) {
        DaoSchoolChild info = new DaoSchoolChild();
        BeanUtil.copyProperties(daoSchoolChildUpdateByIdAdminDTO, info);
        return SqlHelper.retBool(baseMapper.updateById(info));
    }

    /**
     * 对应条目的数据+1
     *
     * @param id
     */
    public void totalAddOne(Long id) {
        UpdateWrapper<DaoSchoolChild> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.setSql("article_total=article_total+1");
        baseMapper.update(new DaoSchoolChild(), updateWrapper);
    }

    /**
     * 对应条目的数据-1
     *
     * @param id
     */
    public void totalSubOne(Long id) {
        UpdateWrapper<DaoSchoolChild> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);
        updateWrapper.setSql("article_total=article_total-1");
        baseMapper.update(new DaoSchoolChild(), updateWrapper);
    }

    /**
     * 根据id获取唯一一条数据
     *
     * @param id
     * @return
     */
    public DaoSchoolChild selectById(Long id) {
        return baseMapper.selectById(id);
    }

    /**
     * 根据id删除指定数据
     *
     * @param id
     * @return
     */
    public Boolean deleteById(Long id) {
        return SqlHelper.retBool(baseMapper.deleteById(id));
    }



    /**
     * 根据父集ID获取子集列表
     *
     * @param daoSchoolChildTopGetListAllDTO
     * @return
     */
    public List<DaoSchoolChildPageAdminVO> selectByTopIdsGetListAll(DaoSchoolChildTopGetListAllDTO daoSchoolChildTopGetListAllDTO) {
        return baseMapper.selectByTopIdsGetListAll(daoSchoolChildTopGetListAllDTO);
    }


    /**
     * 根据父集id,获取子集列表
     * @param topId
     * @return
     */
    public List<DaoSchoolArticleMaterialTopIdVO> selectByTopId(Long topId) {

        return baseMapper.selectByTopId(topId,6L);
    }
}
